package com.tianchi.lexiang.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


import android.os.SystemProperties;
import com.tianchi.lexiang.R;
import android.content.Context;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.VideoView;

public class VolumeControler {
	private static final String TAG = "VolumeControler";
	private VideoView mVideoView;
	private boolean isOrginal;
	private VolumeControler controler;
	
	private static String VCD_LEFT_CHANNEL = "";
	private static String VCD_RIGHT_CHANNEL = "";
	private static String DVD_ORIGINAL = "";
	private static String DVD_ACCOMPANY  = "";
	
	private String songVulme;
	
	public VolumeControler(VideoView video, Context mContext){
		if (controler == null) {
			this.mVideoView = video;
			this.isOrginal = true;//this is default
			this.controler = this;
			Resources resouce = mContext.getResources();
			
			VCD_LEFT_CHANNEL = resouce.getString(R.string.vcd_left_channel);
			VCD_RIGHT_CHANNEL = resouce.getString(R.string.vcd_right_channel);
			DVD_ORIGINAL = resouce.getString(R.string.dvd_original);
			DVD_ACCOMPANY = resouce.getString(R.string.dvd_accompany);
		}
	}
	
	public void setVideoView(VideoView video){
		this.mVideoView = video;
	}
	
	private void toggleChange(boolean orginal){
		Log.d(TAG, "current is "+(orginal?"orginal":"banchang")+";;;and the song is "+songVulme);
		this.isOrginal = orginal;
		switchOriginalAndAccompanyBySongVolume(songVulme);
	}
	
	public void volumeChange(){
		toggleChange(!isOrginal);
	}
	
	public void switchOriginalAndAccompanyBySongVolume(String songVolume){
		this.songVulme = songVolume;
		Log.d(TAG, "[switchOriginalAndAccompanyBySongVolume].....songVolume===>" + songVolume);
		if (VCD_LEFT_CHANNEL.equals(songVolume)) {
			//TODO
			Log.d(TAG, "this song is VCD_LEFT_CHANNEL ,now we can't process");
			if (isOrginal) {
				setLeftOrRightVolume("1");
			}else {
				setLeftOrRightVolume("2");
			}
		}else if(VCD_RIGHT_CHANNEL.equals(songVolume)){
			//TODO
			Log.d(TAG, "this song is VCD_RIGHT_CHANNEL ,now we can't process");
			if (!isOrginal) {
				setLeftOrRightVolume("1");
			}else {
				setLeftOrRightVolume("2");
			}
		}else if (DVD_ORIGINAL.equals(songVolume)) {
			 Log.d(TAG, "this song is DVD_ORIGINAL ,now we can't process");
			setLeftOrRightVolume("0");
			if (isOrginal) {
				setAudioTrackVolume(1);
			}else {
				setAudioTrackVolume(2);
			}
		}else if (DVD_ACCOMPANY.equals(songVolume)) {
			 Log.d(TAG, "this song is DAD_ACCOMPANY ,now we can't process");
			setLeftOrRightVolume("0");
			if (!isOrginal) {
				setAudioTrackVolume(1);
			}else {
				setAudioTrackVolume(2);
			}
		}
	}
	
	private void setLeftOrRightVolume(String value){
		Log.d(TAG, "[setLeftOrRightVolume] value =>" + value);
		SystemProperties.set("sys.audio.mode", value);
	}
	
	private void setAudioTrackVolume(int ar){
		Log.d(TAG, "[setAudioTrackVolume]selectTrack "+ar);
		try {
			Field mediaField = mVideoView.getClass().getDeclaredField("mMediaPlayer");
			mediaField.setAccessible(true);
			Object mediaplayer = mediaField.get(mVideoView);
			if (mediaplayer != null) {
				Method selectTrack = MediaPlayer.class.getDeclaredMethod("selectTrack", int.class);		
				selectTrack.invoke(mediaplayer, ar);
			}else {
				Log.e(TAG, "mediaplayer has not been created , setAudioTrackVolume failed");
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
